package com.dnjn.happiness.biz.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.biz.domain.UserContribution;
import com.dnjn.happiness.biz.service.IUserContributionService;
import com.dnjn.happiness.common.core.constant.Constants;
import com.dnjn.happiness.common.core.constant.SecurityConstants;
import com.dnjn.happiness.common.core.domain.R;
import com.dnjn.happiness.common.core.model.HxBeanSeed;
import com.dnjn.happiness.common.core.utils.poi.ExcelUtil;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.emo.api.RemoteEmoUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 贡献记录Controller
 *
 * @author happiness
 * @date 2023-10-19
 */
@Tag(name = "biz contribution 贡献记录", description = "贡献记录")
@RestController
@RequestMapping("/contribution")
public class UserContributionController extends BaseController {
    @Autowired
    private IUserContributionService userContributionService;

    @Autowired
    private RemoteEmoUserService hxBeanService;


    /**
     * 查询贡献记录列表
     */
    @RequiresPermissions("biz:contribution:list")
    @PostMapping("/list1")
    public AjaxResult list1(@RequestBody UserContribution userContribution, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size).addOrder(OrderItem.desc("curr_datetime"));
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userContribution.getUserId());
        map.put("deptId", userContribution.getDeptId());
        Page<UserContribution> list = userContributionService.getPage(page, map);
        return success(list);
    }


    /**
     * wx查询贡献记录列表
     */
//    @RequiresPermissions("biz:contribution:list")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody UserContribution userContribution, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size).addOrder(OrderItem.desc("curr_datetime"));
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userContribution.getUserId());
        map.put("deptId", userContribution.getDeptId());
        Page<UserContribution> list = userContributionService.getPage(page, map);
//        Page<UserContribution> list = userContributionService.page(page,Wrappers.query(userContribution));
        return success(list);
    }

    /**
     * 导出贡献记录列表
     */
    @Log(title = "贡献记录", businessType = BusinessType.EXPORT)
    @RequiresPermissions("biz:contribution:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserContribution userContribution) {
        List<UserContribution> list = userContributionService.list(Wrappers.query(userContribution));
        ExcelUtil<UserContribution> util = new ExcelUtil<UserContribution>(UserContribution.class);
        util.exportExcel(response, list, "贡献记录数据");
    }

    /**
     * 获取贡献记录详细信息
     */
    @RequiresPermissions("biz:contribution:query")
    @GetMapping(value = "/get/{id}")
    public AjaxResult getUserContribution(@PathVariable("id") Long id) {
        return success(userContributionService.getById(id));
    }


    /**
     * wx获取贡献记录详细信息
     */
//    @RequiresPermissions("biz:contribution:query")
    @GetMapping(value = "/{id}")
    public AjaxResult get(@PathVariable("id") Long id) {
        return success(userContributionService.getById(id));
    }


    /**
     * 新增贡献记录
     */
    @Log(title = "贡献记录", businessType = BusinessType.INSERT)
    @RequiresPermissions("biz:contribution:add")
    @PostMapping("/addUserContribution")
    public AjaxResult addUserContribution(@RequestBody UserContribution userContribution) {
        userContribution.setCreateBy(SecurityUtils.getUsername());
        userContribution.setCurrDatetime(LocalDateTime.now());
        boolean flag = userContributionService.save(userContribution);
        if (flag) {
            HxBeanSeed hxBeanSeed = new HxBeanSeed();
            hxBeanSeed.setUserId(userContribution.getUserId());
            hxBeanSeed.setRiskType(Constants.CONTRIBUTE_HLBEAN_CODE);
            R<Boolean> result = hxBeanService.updateHBean(hxBeanSeed, SecurityConstants.INNER);
        }
        return toAjax(flag);
    }

    /**
     * wx新增贡献记录
     */
    @Log(title = "贡献记录", businessType = BusinessType.INSERT)
//    @RequiresPermissions("biz:contribution:add")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody UserContribution userContribution) {
        userContribution.setCreateBy(SecurityUtils.getUsername());
        userContribution.setCurrDatetime(LocalDateTime.now());
        boolean flag = userContributionService.save(userContribution);
        if (flag) {
            HxBeanSeed hxBeanSeed = new HxBeanSeed();
            hxBeanSeed.setUserId(userContribution.getUserId());
            hxBeanSeed.setRiskType(Constants.CONTRIBUTE_HLBEAN_CODE);
            R<Boolean> result = hxBeanService.updateHBean(hxBeanSeed, SecurityConstants.INNER);
        }
        return toAjax(flag);
    }

    /**
     * 修改贡献记录
     */
    @Log(title = "贡献记录", businessType = BusinessType.UPDATE)
    @RequiresPermissions("biz:contribution:edit")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody UserContribution userContribution) {
        return toAjax(userContributionService.updateById(userContribution));
    }

    /**
     * 删除贡献记录
     */
    @Log(title = "贡献记录", businessType = BusinessType.DELETE)
    @RequiresPermissions("biz:contribution:remove")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            userContributionService.lambdaUpdate().eq(UserContribution::getId, id).set(UserContribution::getDeleted, "1").update();
        }
        return success();
    }

    @Operation(summary = "小程序根据用户Id查询用户贡献记录列表")
    @GetMapping("/getContributionList/{userId}")
    public AjaxResult getContributionList(@PathVariable Long userId) {
        Assert.notNull(userId, "用户id不能为空");
        List<UserContribution> list = userContributionService.getContributionList(userId);
        return AjaxResult.success(list);
    }
}
